#!/bin/sh

curr_path=`dirname $0`
curr_path=`(cd $curr_path; pwd)`

show_help() {
  echo "Usage: configure [options]"
  echo "Options: [defaults in brackets after descriptions]"
  echo
  echo "Standard options:"
  echo "  --help                   print this message"
  echo "  --prefix=PREFIX          install in PREFIX [$PREFIX]"
  echo "  --python-bin=PYTHON-BIN  python binary path"
  exit 1
}


if [ "$1" = -h -o "$1" = -help -o "$1" = --help ]
then
    show_help
fi

PREFIX=/usr/local
OPTS=-O2
PYTHON_CFLGAS=
PYTHON_LIBS=
CC=gcc
CXX=g++
AR=ar
RANLIB=ranlib
STRIP=strip
PYTHON_BIN=

for opt do
    optval="${opt#*=}"
    case "$opt" in
    --prefix=*)
        PREFIX="$optval";
	;;
    --python-bin=*)
        PYTHON_BIN="$optval";
	;;
    --help)
	show_help
	;;
    esac
done

if [ "$PYTHON_BIN" = "" ]; then
	PYTHON_BIN=$PREFIX/bin/python
fi
if [ ! -f $PYTHON_BIN ]; then
	echo "Could not find python from $PYTHON_BIN"
	exit
fi

echo "PREFIX=$PREFIX" > config.mk
echo "CC=$CC" >> config.mk
echo "CXX=$CXX" >> config.mk
echo "AR=$AR" >> config.mk
echo "RANLIB=$RANLIB" >> config.mk
echo "STRIP=$STRIP" >> config.mk
#echo "CFLAGS+=" >> config.mk
#echo "CPPFLAGS+=" >> config.mk

PYTHON_VERSION=`$PYTHON_BIN -c 'import sys;print "%d.%d" % (sys.hexversion >> 24, (sys.hexversion & 0x00ff0000) >> 16)'`
#PYTHON_CFLAGS=-I$SDK_PATH/include/python$PYTHON_VERSION
PYTHON_CFLAGS=`$PYTHON_BIN-config --cflags`
PYTHON_LDFLAGS=`$PYTHON_BIN -c 'import distutils.sysconfig;print distutils.sysconfig.get_config_var("LINKFORSHARED")'`
PYTHON_LIBS="$PREFIX/lib/python$PYTHON_VERSION/config/libpython$PYTHON_VERSION.a -lm -lutil -ldl -lpthread"

echo "PYTHON_BIN=$PYTHON_BIN"  >> config.mk
echo "PYTHON_VERSION=$PYTHON_VERSION"  >> config.mk
echo "PYTHON_CFLAGS=$PYTHON_CFLAGS"  >> config.mk
echo "PYTHON_LDFLAGS=$PYTHON_LDFLAGS"  >> config.mk
echo "PYTHON_LIBS=$PYTHON_LIBS"  >> config.mk


echo "[Desktop Entry]" > noraejoe-daddy.desktop
echo "Exec=$PREFIX/bin/noraejoe-daddy %F" >> noraejoe-daddy.desktop
cat noraejoe-daddy.desktop.in >> noraejoe-daddy.desktop
chmod 644 noraejoe-daddy.desktop

echo "#!/bin/sh" > noraejoe-daddy
echo "cd $PREFIX/share/noraejoe-daddy" >> noraejoe-daddy
echo "export LD_LIBRARY_PATH=$PREFIX/lib:$LD_LIBRARY_PATH" >> noraejoe-daddy
echo "exec python noraejoe-daddy.pyc \$@" >> noraejoe-daddy
chmod a+x noraejoe-daddy

